package com.skirmisher.inspection.daily.controller;

import com.skirmisher.inspection.common.utils.PageUtils;
import com.skirmisher.inspection.daily.entity.*;
import com.skirmisher.inspection.daily.service.InsDailyJobService;
import com.skirmisher.inspection.inspection.testController.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName InsDailyJobController
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/13 下午2:51
 * @Version 1.0
 */
@Api(tags = "日常巡查任务接口")
@RestController
@RequestMapping("/dailyJob")
public class InsDailyJobController {

    @Autowired
    private InsDailyJobService insDailyJobService;

    @ApiOperation(value = "获取巡查任务详情")
    @ApiImplicitParam(name = "jobId",value = "任务id",required = true)
    @GetMapping(value = "/getJobDetail")
    public ResponseModel getJobDetail(String jobId){
        ResponseModel responseModel = new ResponseModel();
        if (StringUtils.isEmpty(jobId)){
            responseModel.setCode(201);
            responseModel.setMessage("无模板id");
            return responseModel;
        }
        InsDailyJob job = insDailyJobService.getJobDetail(jobId);
        responseModel.setData(job);
        return  responseModel;
    }

    @ApiOperation(value = "日常巡查任务页面查询")
    @GetMapping(value = "/getJobPageData")
    public ResponseModel getJobPageData(InsDailyJobQueryEntity query){

        ResponseModel responseModel = new ResponseModel();
        //参数校验
        Integer startRow = PageUtils.getStartRow(query.getPage(), query.getRows());
        if (startRow == null || startRow < 0){
            responseModel.setCode(201);
            responseModel.setMessage("分页参数有误");
            return  responseModel;
        }
        query.setStartRows(startRow);
        insDailyJobService.getJobPageData(query);
        responseModel.setDataDataGrid(query.getDateList(), query.getCount());


        return responseModel;
    }

    /**
     * @Author hanwangxin
     * @Description //TODO 批量删除巡检任务 * @Date 下午9:55 2020/12/12
     * @Param [tempIds]
     * @return com.skirmisher.inspection.inspection.testController.ResponseModel
     **/
    @ApiOperation(value = "批量删除巡检任务")
    @ApiImplicitParam(name = "jobIds",value = "任务id集合",required = true)
    @PostMapping(value = "/deletDaliyJob")
    public ResponseModel deletDaliyJob(@RequestBody List<String> jobIds){
        ResponseModel responseModel = new ResponseModel();
        insDailyJobService.deletDaliyJob(jobIds);
        return responseModel;
    }


    @ApiOperation(value = "保存日常巡查任务")
    //@ApiOperationSupport(ignoreParameters = {"name"})
    @PostMapping(value = "/saveDailJob")
    public ResponseModel saveDailJob(@RequestBody @Validated InsDailyJobSave jobSave){

        ResponseModel responseModel = new ResponseModel();

        insDailyJobService.saveDailyJob(jobSave,responseModel);
        return  responseModel;
    }

    @ApiOperation(value = "更新巡查任务")
    //@ApiOperationSupport(ignoreParameters = {"name"})
    @PostMapping(value = "/updateDaliyJob")
    public ResponseModel updateDaliyJob(@RequestBody @Validated InsDailyJobSave jobSave){

        ResponseModel responseModel = new ResponseModel();

        String id = jobSave.getId();
        if (StringUtils.isEmpty(id)){
            responseModel.setCode(201);
            responseModel.setMessage("无任务id");
            return responseModel;
        }

        insDailyJobService.updateDaliyJob(jobSave,responseModel);
        return  responseModel;

    }

    @ApiOperation(value = "日常巡查任务派发")
    //@ApiOperationSupport(ignoreParameters = {"name"})
    @PostMapping(value = "/updateDaliyJob2Send")
    public ResponseModel updateDaliyJob2Send(@RequestBody @Validated InsDailyJobSave jobSave){

        ResponseModel responseModel = new ResponseModel();

        String id = jobSave.getId();
        if (StringUtils.isEmpty(id)){
            responseModel.setCode(201);
            responseModel.setMessage("无任务id");
            return responseModel;
        }

        insDailyJobService.updateConformJob2Send(jobSave,responseModel);
        return  responseModel;

    }

}
